<?php
$obj = new stdClass();
$obj->hoteldates = array();

$counter = 0;
$groupCounter = 0;

foreach ($data as $item) {
	
	$hotelDates = $item->getRelated('hotelDates');
	
	$groupCounter++;
	
	if (count($hotelDates) > 0) {
		foreach ($hotelDates as $subItem) {
			$counter++;
			
			$subObj = new stdClass();
			$subObj->id        = $subItem->id;
			$subObj->dateStart = $subItem->dateStart;
			$subObj->dateEnd   = $subItem->dateEnd;
			$subObj->group     = '#' . $groupCounter;
			$subObj->groupId   = $item->id;
			$subObj->action    = 'icon-delete-item';
			
			array_push($obj->hoteldates, $subObj);
		}
	}
}
$obj->totalCount = $counter; 

echo json_encode($obj);